$("#main").on("click",".order-title-btn",function(){
	var _attr=$(this).attr("_attr");
    var href=location.href.substring(0, location.href.indexOf('=')+1);
	console.log(href+_attr)
	location.replace(href+_attr);
})
var objOTB=$(".order-title-btn")
var href_id=location.href.charAt(location.href.length-1)
for (var i=0;i<objOTB.length;i++){
	if(objOTB.eq(i).attr("_attr")==href_id){
        objOTB.eq(i).addClass("thison")
	}
}
var page=1;
getOrder(page);

function getOrder(page){
    var id=getUrlParam("id");
    if(!id)
    {
        id=0;
    }
	$.ajax({
        type: "get",
        url: Url + "order/getOrder/" + id + "/page/" + page,
        async: false,
        timeout : 20000,
        beforeSend: function (xhr) {
            //loadData1(defaultData1)

                showDiv()

            xhr.setRequestHeader("token", tokenk);
        },
        success: function (data) {
            hiddenDiv()
                    console.log(data)
                    if (data.length == 0) {
                        $(".discount-main-no").css("display", "block")
                    }
                    var str = '';
                    for (var i = 0; i < data.length; i++) {
                        var msg = '';
                        var clickz = '';
                        if (data[i].order_states == 1) {
                            msg = '待付款';
                            clickz = '<div class="op-ready goPay" _id="' + data[i].order_number + '">付款</div>';
                        } else if (data[i].order_states == 2) {
                            msg = '等待发货';
                            clickz = '<div class="op-ready remindGoods" _id="' + data[i].order_id + '">提醒发货</div>';
                        } else if (data[i].order_states == 3) {
                            msg = '已发货';
                            clickz = '<div class="op-ready orderLogistics" _id="' + data[i].order_id + '" _amount="' + data[i].evaluate_arr + '">查看物流</div><div class="op-ready  goodsReceipt" _id="' + data[i].order_id + '">确认收货</div>';
                        }
                        else if (data[i].order_states == 5) {
                            msg = '订单已取消';
                            clickz = '';
                        }
                        else if (data[i].order_states == 6) {
                            msg = '待评价';
                            console.log(data[i].order_id)
                            clickz = '<div class="op-ready"><a href="review.html?id=' + data[i].order_id + '">评价</a></div>';
                        } else if (data[i].order_states == 7) {
                            msg = '订单已完成';
                            clickz = '';
                        }
                        //
                        str += '<div class="order-main">'

                            + '<div class="order-num clear"><span class="order-num-l">订单编号：' + data[i].order_number + '</span><span  class="order-num-r">' + msg + '</span></div>'
                            + '<a href="order_details.html?id=' + data[i].order_id + '">'
                            + '<div class="order-details clear">'
                            + '<img class="od-img" src="' + data[i].snap_img + '"></img>'
                            + '<div class="od-mian" >'
                            + '<span>' + data[i].snap_name + '</span>'
                            + '</div>'
                            + '<div class="od-state" >'
                            + '<span>共' + data[i].total_count + '件商品</span>'
                            + '</div>'
                            + '</div>'
                            + '</a>'
                            + '<div class="order-price clear">'
                            + '<div class="op-price">应付：' + data[i].total_price + '元</div>' + clickz
                            + '</div>'

                            + '</div>'
                    }
                    $("#order-main-list").html(str)
                },
                error: function (request, status, err) {
                    failure(status)
                }
            });
        }



    $(".goPay").on('click', function () {
        var id = $(this).attr('_id');
        $.ajax({
            type: "post",
            url: Url + "pay/pre_order",
            data: {order_no: id},
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                var datas = JSON.stringify(data);
                var token_key = randomString(12);
                storage.setItem(token_key, datas);

                location.href = "http://admin.chinlingcloud.com/index/index/go_pay.html?id=" + token_key;
                return false;
            },
            error: function (data,status) {
                var datas = JSON.stringify(data);
                failure(status)
            },
            complete: function () {

            },
        });
    })

//查看物流
    $(".orderLogistics").on('click', function () {
        var id = $(this).attr('_id');
        var amount = $(this).attr('_amount');
        if (amount != 1) {
            location.href = "logisticsinfo_ex.html?id=" + id;
        } else {
            location.href = "logistics_information.html?id=" + id;
        }
    })
    $(".remindGoods").on('click', function () {
        var id = $(this).attr('_id');
        $.ajax({
            type: "post",
            url: Url + "order/remind",
            data: {order_id: id},
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                if (data) {
                    taostr('提醒成功',1000);
                } else {
                    taostr('已提醒',1000);
                }
            },
            error: function (request, status, err) {
                failure(status)
            },
            complete: function () {

            },
        });
    })
//确认收货
    $(".goodsReceipt").on('click', function () {
        var id = $(this).attr('_id');
        $.ajax({
            type: "post",
            url: Url + "order/goodsReceipt",
            data: {order_id: id},
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                if (data) {
                    //跳转评价
                    taostr('确认收货成功',1000);
                    location.href = "review.html?id=" + data;
                } else {
                    taostr('确认收货失败',1000);
                }
            },
            error: function (request, status, err) {
                failure(status)
            },
            complete: function () {

            },
        });
    })


slideDown("#main", 61, function (e) {
    var that = this;
    getOrder(page)
    that.back.call();

});
slideUp("#main", 61, function (e) {

    var that = this;
    page++;
    getOrder(page)
    that.backup.call();
});


